package it.univpm.diiga.memorysim.views;


import it.univpm.diiga.memorysim.data.DataPlugin;
import it.univpm.diiga.memorysim.frames.process.Detail;

import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.part.ViewPart;
import org.eclipse.swt.graphics.Rectangle;

public class OutputSimulation extends ViewPart {

	public static final String ID = "it.univpm.diiga.memorysim.views.OutputSimulation";
	private static int id_sel=-1;
	private Composite top = null;
	private ScrolledComposite sc1 = null;
	private static Composite c1;
	private static Composite c = null;
	private static Button d; 
	private int row_element=15;
	private int x=20;
	private int y=20;
	private int x_a=30;
	private int y_a=25;
	private int w=10;
	private int l=18;
	private int NmemC=0;
	private int NmemS=0;
	private Composite sc;
	private static int[] colorC;
	private static int[] colorS;
	private static String[] labelC;
	private static String[] labelS;
	private static final int[] COLOR_INDICES = {SWT.COLOR_GRAY, SWT.COLOR_DARK_BLUE, SWT.COLOR_GREEN,
	      SWT.COLOR_DARK_MAGENTA};
	 
	public void createPartControl(Composite parent) {
		int lenghtS=0;
		int lenghtC=0;
		if(DataPlugin.blockdata>=1){
			NmemC=(DataPlugin.memorycentraldata*1024)/DataPlugin.blockdata;
			NmemS=(DataPlugin.swapdata*1024)/DataPlugin.blockdata;
		}
		colorC=new int[NmemC];
		colorS=new int[NmemS];
		labelC=new String[NmemC];
		labelS=new String[NmemS];
		setDefaultColor();
		setDefaultLabels();
		lenghtS=(NmemS/row_element)*50;
		lenghtC=(NmemC/row_element)*50;
		final Shell shell = getViewSite().getShell();
		GridLayout gridLayout21 = new GridLayout();
		gridLayout21.numColumns = 10;
		gridLayout21.makeColumnsEqualWidth = false;
		GridData gridData11 = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
		gridData11.heightHint = -1;
		gridData11.horizontalAlignment = GridData.FILL;
		gridData11.verticalAlignment = GridData.FILL;
		gridData11.grabExcessHorizontalSpace = true;
		gridData11.grabExcessVerticalSpace = true;
		gridData11.horizontalIndent = 0;
		gridData11.widthHint = -1;
		GridData gridData = new GridData();
		gridData.grabExcessVerticalSpace = false;
		
		GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 3;
		
		top = new Composite(parent, SWT.NONE);
		top.setLayout(null);
		String element="";
		if(DataPlugin.SimSelected=="Paginazione") element=" pagina"; 
		if(DataPlugin.SimSelected=="Segmentazione") element=" segmentazione";
		Label l0 = new Label(top,SWT.NONE);
		l0.setText(" processo\n"+element);
		l0.setBounds(new Rectangle(150, 10, 100, 30));
		l0.setBackground(shell.getDisplay().getSystemColor(SWT.COLOR_GREEN));
		d = new Button(top, SWT.NONE);
		d.setText("Dettagli Processo");
		d.setBounds(new Rectangle(300, 10, 150, 30));
		d.setEnabled(false);
		d.addSelectionListener(new SelectionListener(){

					public void widgetDefaultSelected(SelectionEvent e) { }
					
					public void widgetSelected(SelectionEvent e) {
						new Detail(id_sel).run();
					}	
				});
		Label l03 = new Label(top,SWT.NONE);
		l03.setText("Memoria Centrale");
		l03.setBounds(new Rectangle(20, 31, 100, 15));
		Label l02 = new Label(top,SWT.NONE);
		l02.setText("Area di Swap");
		l02.setBounds(new Rectangle(474, 31, 100, 15));

		createSc1();
		drawMemCentral(shell);
		sc1.setContent(c1);
		sc1.setExpandHorizontal(true);
		sc1.setExpandVertical(true);
		sc1.setMinSize(10, lenghtC);
		
		
		sc = new ScrolledComposite(top, SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
		sc.setLayout(new GridLayout());
		c = new Composite(sc, SWT.NONE);
		c.setLayout(gridLayout21);
		sc.setBounds(new Rectangle(474, 51, 440, 313));
		
		drawMemSwap(shell);
		((ScrolledComposite) sc).setContent(c);
		((ScrolledComposite) sc).setExpandHorizontal(true);
		((ScrolledComposite) sc).setExpandVertical(true);
		((ScrolledComposite) sc).setMinSize(10, lenghtS);
		
	}


	@Override
	public void setFocus() { }


	private void createSc1() {
		sc1 = new ScrolledComposite(top, SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
		sc1.setLayout(new GridLayout());
		sc1.setBounds(new Rectangle(20, 51, 440, 313));
		createC1();
	}


	private void createC1() {
		GridLayout gridLayout31 = new GridLayout();
		gridLayout31.numColumns = 10;
		gridLayout31.makeColumnsEqualWidth = false;
		c1 = new Composite(sc1, SWT.NONE);
		c1.setLayout(gridLayout31);
	}

	
		
	private void drawMemCentral(final Shell shell) {
//c1 -> memoria centrale
		
		c1.addPaintListener(new PaintListener() {
			public void paintControl(PaintEvent event) {
				
				double nA=Double.parseDouble(NmemC+"")/row_element;
				int nB=NmemC/row_element;
				if(nA>nB) nB++;
				int n=0;
				for(int i=0;i<nB;i++){
					int n1=x+(x_a*i);
					for(int j=0;j<row_element;j++,n++){
						if(n<NmemC){
						int n2=y+(y_a*j);
						event.gc.setForeground(shell.getDisplay().getSystemColor(colorC[n]));
						event.gc.setLineWidth(10);
						event.gc.drawRectangle(n2,n1 , w, l);
					    event.gc.setForeground(shell.getDisplay().getSystemColor(COLOR_INDICES[0]));
					    event.gc.drawText(labelC[n],n2-2,n1-5,true);
						}
					}
				}
			}

		});
		
	}

	public void drawMemSwap(final Shell shell){
		//c -> swap
		c.addPaintListener(new PaintListener() {
			public void paintControl(PaintEvent event) {
				double nA=Double.parseDouble(NmemS+"")/row_element;
				int nB=NmemS/row_element;
				if(nA>nB) nB++;
				int n=0;
				for(int i=0;i<nB;i++){
					int n1=x+(x_a*i);
					for(int j=0;j<row_element;j++,n++){
						if(n<NmemS){
						int n2=y+(y_a*j);
						event.gc.setForeground(shell.getDisplay().getSystemColor(colorS[n]));
						event.gc.setLineWidth(10);
						event.gc.drawRectangle(n2,n1,w, l);
					    event.gc.setForeground(shell.getDisplay().getSystemColor(COLOR_INDICES[0]));
					    event.gc.drawText(labelS[n],n2-2,n1-5,true);
						}
					}
				}
			}
		});

	}
	
	public static void setPageFoult(int pid, int in, int out){
		id_sel=pid;
		d.setEnabled(true);
		 for (int i=0;i<colorC.length;i++){
			 if(i==in){
				 colorC[i]=COLOR_INDICES[2];
			 }else colorC[i]=COLOR_INDICES[3];
		 }
		 for (int i=0;i<colorS.length;i++){
			 if(i==out){
				 colorS[i]=COLOR_INDICES[2];
			 }else colorS[i]=COLOR_INDICES[1];
		 }
		 c.redraw();
		 c1.redraw();
	}

	public void setDefaultColor(){
		 for (int i=0;i<colorC.length;i++){
			 colorC[i]=COLOR_INDICES[3];
		 }
		 for (int i=0;i<colorS.length;i++){
			 colorS[i]=COLOR_INDICES[1];
			 } 
	}
	public void setDefaultLabels(){
		 for (int i=0;i<labelC.length;i++){
			 labelC[i]=i+"";
		 }
		 for (int i=0;i<labelS.length;i++){
			 labelS[i]=i+"";
			 } 
	}
	
	public static void setNewLabels(String[] memlabels, String[] swaplabels){
		 labelC=memlabels;
	     labelS=swaplabels;
		 c.redraw();
		 c1.redraw();
	}
} 

